﻿using Microsoft.Extensions.Logging;
using ORS.OrderRobot.IOC;
using ORS.OrderRobot.ServiceContracts;
using ORS.OrderRobot.ServiceContracts.Dtos;
using ORS.OrderRobot.ServiceContracts.Dtos.Api;
using ORS.OrderRobot.ServiceContracts.ThirdPartApi;

namespace ORS.OrderRobot.Service.ThridPartApi
{
	public class ThirdSystemService: IThirdSystemService,ISingleton
    {
        private readonly IHttpHelper _httpHelper;
        private readonly ILogger<ThirdSystemService> _logger;
        public ThirdSystemService(
            IHttpHelper httpHelper,
            ILogger<ThirdSystemService> logger
        )
        {
            _httpHelper = httpHelper;
            _logger = logger;
        }

        public async Task<List<FlightInfoDto>> FetchFlightInfo(string date,string time)
        {
            date = date.Replace("-", "/");
            var url = $"https://www.kixdutyfree.jp/on/demandware.store/Sites-KixDutyFree-Site/zh_CN/Flight-GetInfo?date={date}&time={time}";
            var httpResult = await _httpHelper.SendAndParseAsync<FlightInfoResDto>(url,HttpMethod.Get);
            if(httpResult.StatusCode != 200 || httpResult.Body == null)
            {
                _logger.LogError("同步航班数据失败");
                throw new Exception("同步航班数据失败");
            }
            return httpResult.Body.FlightData;
        }
    }
}

